package com.luckcoder.relax.games.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.luckcoder.relax.games.domain.bo.RelaxBlessingConfigBo;
import com.luckcoder.relax.games.domain.vo.RelaxBlessingConfigVo;
import com.luckcoder.relax.games.domain.RelaxBlessingConfig;
import com.luckcoder.relax.games.mapper.RelaxBlessingConfigMapper;
import com.luckcoder.relax.games.service.IRelaxBlessingConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 祝福语配置Service业务层处理
 *
 * @author zhanghao
 * @date 2025-09-11
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RelaxBlessingConfigServiceImpl implements IRelaxBlessingConfigService {

    private final RelaxBlessingConfigMapper baseMapper;

    /**
     * 查询祝福语配置
     *
     * @param blessingId 主键
     * @return 祝福语配置
     */
    @Override
    public RelaxBlessingConfigVo queryById(Long blessingId){
        return baseMapper.selectVoById(blessingId);
    }

    /**
     * 分页查询祝福语配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 祝福语配置分页列表
     */
    @Override
    public TableDataInfo<RelaxBlessingConfigVo> queryPageList(RelaxBlessingConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RelaxBlessingConfig> lqw = buildQueryWrapper(bo);
        Page<RelaxBlessingConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的祝福语配置列表
     *
     * @param bo 查询条件
     * @return 祝福语配置列表
     */
    @Override
    public List<RelaxBlessingConfigVo> queryList(RelaxBlessingConfigBo bo) {
        LambdaQueryWrapper<RelaxBlessingConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RelaxBlessingConfig> buildQueryWrapper(RelaxBlessingConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RelaxBlessingConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RelaxBlessingConfig::getBlessingId);
        lqw.eq(StringUtils.isNotBlank(bo.getGameCode()), RelaxBlessingConfig::getGameCode, bo.getGameCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBlessingText()), RelaxBlessingConfig::getBlessingText, bo.getBlessingText());
        lqw.eq(StringUtils.isNotBlank(bo.getBlessingType()), RelaxBlessingConfig::getBlessingType, bo.getBlessingType());
        lqw.eq(StringUtils.isNotBlank(bo.getBlessingCategory()), RelaxBlessingConfig::getBlessingCategory, bo.getBlessingCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguage()), RelaxBlessingConfig::getLanguage, bo.getLanguage());
        lqw.eq(bo.getWeight() != null, RelaxBlessingConfig::getWeight, bo.getWeight());
        lqw.eq(bo.getSortOrder() != null, RelaxBlessingConfig::getSortOrder, bo.getSortOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), RelaxBlessingConfig::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEnabled()), RelaxBlessingConfig::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }

    /**
     * 新增祝福语配置
     *
     * @param bo 祝福语配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RelaxBlessingConfigBo bo) {
        RelaxBlessingConfig add = MapstructUtils.convert(bo, RelaxBlessingConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBlessingId(add.getBlessingId());
        }
        return flag;
    }

    /**
     * 修改祝福语配置
     *
     * @param bo 祝福语配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RelaxBlessingConfigBo bo) {
        RelaxBlessingConfig update = MapstructUtils.convert(bo, RelaxBlessingConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RelaxBlessingConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除祝福语配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
